package JDBCUI;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Huodebrowse extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet res = null;
    JTable jtable;
    JScrollPane jscrollpane = new JScrollPane();

    Vector columnNames = null;
    Vector rowData = null;

    public Huodebrowse() {

        browse  bs= new browse();

        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        columnNames = new Vector();
        columnNames.add("教师  编号：");
        columnNames.add("教师  姓名：");


        rowData = new Vector();


        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");
            ps = conn.prepareStatement("SELECT * FROM allstaff");
            res = ps.executeQuery();
            while (res.next())
            {
                Vector hang = new Vector();
                hang.add(res.getString(1));
                hang.add(res.getString(2));





                rowData.add(hang);

            }
            System.out.println("浏览成功!");
        }catch (Exception q){
            q.printStackTrace();
            System.out.println("浏览异常！！！");
        }finally{
            try{
                res.close();
                ps.close();
                conn.close();
                System.out.println("浏览操作结束");
            }catch (SQLException o){
                o.printStackTrace();
                System.out.println("资源关闭异常");
            }
        }
        jtable = new JTable(rowData,columnNames);
        jscrollpane = new JScrollPane(jtable);

        this.add(jscrollpane);
        this.setTitle("教师编号浏览表");
        this.setLayout(new GridLayout(2,5));
        this.add(jpforbutton);
        this.setLocation(100,400);
        this.setSize(500,300);
        this.setVisible(true);
        this.setResizable(false);
    }
}
